package com.example.wechat.utils;

import android.content.Context;
import android.content.SharedPreferences;

public class UserManager {
    private static final String PREF_NAME = "user_pref";
    private static final String KEY_USER_ID = "user_id";
    private static final String KEY_USERNAME = "username";
    private static final String KEY_TOKEN = "token";
    private static final String KEY_PHONE = "phone";
    private static final String KEY_EMAIL = "email";
    private static final String KEY_AVATAR = "avatar";
    
    private static UserManager instance;
    private final SharedPreferences prefs;
    
    private UserManager(Context context) {
        prefs = context.getApplicationContext()
                .getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    }
    
    public static synchronized UserManager getInstance(Context context) {
        if (instance == null) {
            instance = new UserManager(context);
        }
        return instance;
    }
    
    public void saveUserInfo(String userId, String username, String token, 
                           String phone, String email, String avatar) {
        prefs.edit()
            .putString(KEY_USER_ID, userId)
            .putString(KEY_USERNAME, username)
            .putString(KEY_TOKEN, token)
            .putString(KEY_PHONE, phone)
            .putString(KEY_EMAIL, email)
            .putString(KEY_AVATAR, avatar)
            .apply();
    }
    
    public void clearUserInfo() {
        prefs.edit().clear().apply();
    }
    
    public String getUserId() {
        return prefs.getString(KEY_USER_ID, null);
    }
    
    public String getUsername() {
        return prefs.getString(KEY_USERNAME, null);
    }
    
    public String getToken() {
        return prefs.getString(KEY_TOKEN, null);
    }
    
    public String getPhone() {
        return prefs.getString(KEY_PHONE, null);
    }
    
    public String getEmail() {
        return prefs.getString(KEY_EMAIL, null);
    }
    
    public String getAvatar() {
        return prefs.getString(KEY_AVATAR, null);
    }
    
    public boolean isLoggedIn() {
        return getToken() != null;
    }
} 